/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.configuration.beans;

import edu.soc.cs5226.gui.entities.util.Constants;
import edu.soc.cs5226.gui.configuration.beans.Configuration;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
 *
 * @author Huimin
 */
public class ConfigurationDAO {
    
    public Configuration getConfiguration()throws SQLException, NamingException{
        
        Configuration defaultConfig = null;
        
        //Retrieve Data from Database
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup(Constants.JAVADB);
        Connection conn = ds.getConnection();
        
        try {
            PreparedStatement pstmt = conn.prepareStatement ("select * from configuration");
                        
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                defaultConfig = new Configuration();
                defaultConfig.setBc_redThreshold(rs.getLong("bc_redThreshold"));
                defaultConfig.setBc_yellowThreshold(rs.getLong("bc_yellowThreshold"));
                defaultConfig.setLb_redThreshold(rs.getLong("lb_redThreshold"));
                defaultConfig.setLb_yellowThreshold(rs.getLong("lb_yellowThreshold"));
                defaultConfig.setS_redThreshold(rs.getLong("s_redThreshold"));
                defaultConfig.setS_yellowThreshold(rs.getLong("s_yellowThreshold"));
                defaultConfig.setRs_redThreshold(rs.getLong("rs_redThreshold"));
                defaultConfig.setRs_yellowThreshold(rs.getLong("rs_yellowThreshold"));
                defaultConfig.setSps_redThreshold(rs.getLong("sps_redThreshold"));
                defaultConfig.setSps_yellowThreshold(rs.getLong("sps_yellowThreshold"));
                defaultConfig.setXValue(rs.getLong("XValue"));
                defaultConfig.setYValue(rs.getLong("YValue"));
            } 
        }
        catch (SQLException se)
        {
            System.out.println("SQL exception in retrieving configuration");
            se.printStackTrace();
        }
        finally {
            conn.close();
            return defaultConfig;
        }             
    }
    
    public String saveConfiguration(Configuration saveConfig)throws SQLException, NamingException{
        
        //Save Data into Database
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup(Constants.JAVADB);
        Connection conn = ds.getConnection();
        
        try {
            System.out.println("BC_Red: " + saveConfig.getBc_redThreshold() + " BC_Y: " + saveConfig.getBc_yellowThreshold() 
                    + " S_Red: " + saveConfig.getS_redThreshold() + " S_Y: " + saveConfig.getS_yellowThreshold()
                    + " LB_Red: " + saveConfig.getLb_redThreshold() + " LB_Y: " + saveConfig.getLb_yellowThreshold()
                    + " RS_Red: " + saveConfig.getRs_redThreshold() + " RS_Y: " + saveConfig.getRs_yellowThreshold()
                    + " SPS_Red: " + saveConfig.getSps_redThreshold() + " SPS_Y: " + saveConfig.getSps_yellowThreshold()
                    + " X: " + saveConfig.getXValue() + " Y: " + saveConfig.getYValue());
            
            PreparedStatement pstmt = conn.prepareStatement ("update configuration set bc_redThreshold = ?, bc_yellowThreshold = ?, "
                  + "s_redThreshold = ?, s_yellowThreshold = ?, lb_redThreshold = ?, lb_yellowThreshold = ?, rs_redThreshold = ?, rs_yellowThreshold = ?, "
                  + "sps_redThreshold = ?, sps_yellowThreshold = ?, XValue = ?, YValue = ?");
           
            pstmt.setLong(1, saveConfig.getBc_redThreshold());
            pstmt.setLong(2, saveConfig.getBc_yellowThreshold());
            pstmt.setLong(3, saveConfig.getS_redThreshold());
            pstmt.setLong(4, saveConfig.getS_yellowThreshold());
            pstmt.setLong(5, saveConfig.getLb_redThreshold());
            pstmt.setLong(6, saveConfig.getLb_yellowThreshold());
            pstmt.setLong(7, saveConfig.getRs_redThreshold());
            pstmt.setLong(8, saveConfig.getRs_yellowThreshold());
            pstmt.setLong(9, saveConfig.getSps_redThreshold());
            pstmt.setLong(10, saveConfig.getSps_yellowThreshold());
            pstmt.setLong(11, saveConfig.getXValue());
            pstmt.setLong(12, saveConfig.getYValue());
            pstmt.execute();
        }
        catch (SQLException se)
        {
            System.out.println("SQL exception in updating configuration");
            se.printStackTrace();
        }
        finally {
            conn.close();
            return ("Successful!");
        }             
    }
}
